package basic.study.algorithms.struct.BST;
import basic.study.algorithms.struct.BST.TreeNode;

/**
 * @author Kevin
 * @date 2020/3/6 15:28
 */
public class Judge_BST {
    public boolean isVAildBST(TreeNode root) {
        return helper(root, null, null);
    }

    public boolean helper(TreeNode node, Integer lower, Integer higher) {
        if (node == null) return true;

        if (lower != null && node.val <= lower) return false;
        if (higher != null && node.val >= higher) return false;

        if (! helper(node.left, lower, node.val)) return false;
        if (!helper(node.right, node.val, higher)) return false;

        return true;
    }

}
